package com.shengyuan.manage_os.old.ying.erpm.db;

import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;

import com.shengyuan.manage_os.old.ying.boce.http.FormFile;
import com.shengyuan.manage_os.old.ying.erpm.MainApplication;
import com.shengyuan.manage_os.R;

public class CLS_Actual_Data_Table {
	public String  JSON1,JSON2, OVERTIME, USER, FORM_TYPE; 
	public int Id, FINISHED, SENDED, PRINTEABLED, PCOUNT, SERVICEID;
	
	public FormFile[] mFilesLists;
	public ArrayList<String> A_ImagesFiles, A_VideoFiles, A_VoiceFiles;
	
	private  void toCreateFile(File file) {
		  try {
		   OutputStream os = new FileOutputStream(file);
		   int bytesRead = 0;
		   byte[] buffer = new byte[2];
		   buffer[0]=buffer[1]=0x20;
		   os.close();
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
	}
	public  void getFormFile_AllFile() {
		int i, k ;
		Json1AndJson2_Files();
		File file;String sfile;
		k = A_ImagesFiles.size() + A_VoiceFiles.size() + A_VideoFiles.size();
		if (k == 0) {
			return ;
		}
		mFilesLists = new FormFile[k];
//        if ( k > 0) {//上传类多文件上传有问题，所以多加一个结束文件
//        	file = new File(MainApplication.AppRoot+"x");
//        	toCreateFile(file);
//			FormFile f = new FormFile(file.getName(), file, "image", "application/error");
//			mFilesLists[k] = f;
//		}
        for (i = 0; i < A_VideoFiles.size(); i++) {
			sfile = A_VideoFiles.get(i);
			file = new File(sfile);
			if (file.exists()) {
				FormFile f = new FormFile(sfile, file, "3gp", "application/octet-stream");
				mFilesLists[i] = f;
			}
        }
        
        k = i;
        for (i = 0; i < A_VoiceFiles.size(); i++) {
			sfile = A_VoiceFiles.get(i);
			file = new File(sfile);
			if (file.exists()) {
				FormFile f = new FormFile(sfile, file, "amr", "audio/amr");
				mFilesLists[i+k] = f;
			}
        }
        
        k = k + i;        
        for (i = 0; i < A_ImagesFiles.size(); i++) {
        	sfile = A_ImagesFiles.get(i);
			file = new File(sfile);
			if (file.exists()) {
				FormFile f = new FormFile(sfile, file, "image", "application/octet-stream");
				mFilesLists[i+k] = f;
			}
		}
	}
	
	public  void ClearFiles() {
		if (mFilesLists!=null) {
			for (int i = 0; i < mFilesLists.length-1; i++) {
				mFilesLists[i].getFile().delete();
			}
		}
	}
	
	private void Json1AndJson2_Files() {
		A_ImagesFiles= new ArrayList<String>();
		A_VideoFiles = new ArrayList<String>();
		A_VoiceFiles = new ArrayList<String>();
		if (JSON1!=null && !JSON1.equals("")) {
			FilesAdd(JSON1, "/json1/");
		}
		if (JSON2!=null && !JSON2.equals("")) {
			FilesAdd(JSON2, "/json2/");
		}
	}
	
	public void DeletePath() {
		String subpath = MainApplication.filePath + Id;
		File f = new File(subpath);
		f.delete();
	}
	
	private void FilesAdd( String jsonString, String subpath )
	{
		Context context = MainApplication.Instance;
		String video= context.getString(R.string.title_actual_form_VideoButton_Video);
		String voice = context.getString(R.string.title_actual_form_RecordButton_Record);
		String picture = context.getString(R.string.title_actual_form_PictureButton_Image);		
		subpath = MainApplication.filePath + Id + subpath;
		try {
			JSONObject json = new JSONObject(jsonString);
			Iterator<String> it = json.keys();
			while (it.hasNext()) {
				String key = it.next().toString();
				String value =  json.getString(key);
				if (key.equals(voice)) {
					value = subpath + value;
					if((new File(value)).exists()){
						A_VoiceFiles.add(value);
					}
				}
				if (key.equals(video)) { 
					value = subpath + value;
					if((new File(value)).exists()){
						A_VideoFiles.add(value);
					}
				}
				if (key.contains(picture)) { 
					value = subpath + value;
					if((new File(value)).exists()){
						A_ImagesFiles.add(value);
					}					
				}
			}
			if (A_ImagesFiles.size()==0) {
				File files= new File(subpath);
				File[] ffFiles = files.listFiles(new ImgFilter());
				if (ffFiles!=null) {
					for (int i = 0; i < ffFiles.length; i++) {
						A_ImagesFiles.add(ffFiles[i].getAbsolutePath());
					}	
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public class ImgFilter implements FileFilter{

		@Override
		public boolean accept(File pathname) {
			String filename = pathname.getName().toLowerCase();
			if(filename.contains(".jpg")){
				return true;
			}else{
				return false;
			}
		}
	}
}
